package com.chen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.bean.Employee;
import com.chen.common.R;
import com.chen.exception.CustomException;
import com.chen.mapper.EmployeeDao;
import com.chen.service.EmployeeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpSession;

@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeDao, Employee> implements EmployeeService {

    /**
     * 登录验证
     *
     * @param emp
     * @return
     */
    @Override
    public R<Employee> logIn(Employee emp, HttpSession session) {
        //密码加密
        String password = DigestUtils.md5DigestAsHex(emp.getPassword().getBytes());
        //
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getUsername, emp.getUsername());
        Employee employee = getOne(wrapper);
        //判断账号是否存在
        if (employee == null) {
            return R.error("用户不存在");
        }
        if (employee.getStatus() == 0) {
            return R.error("用户被禁用");
        }
        if (!password.equals(employee.getPassword())) {
            return R.error("用户密码错误");
        }
        session.setAttribute("employee", employee.getId());

        return R.success(employee);
    }

    /**
     * 后台退出
     *
     * @param session
     * @return
     */
    @Override
    public R<String> logOut(HttpSession session) {
        //清除session对象
        session.invalidate();
        return R.success("退出成功");
    }

    /**
     * 新增员工
     *
     * @param emp
     * @param session
     * @return
     */
    @Override
    public R<String> add(Employee emp, HttpSession session) {
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getUsername, emp.getUsername());
        Employee employee = getOne(wrapper);
        if (employee != null) {
            throw new ClassCastException("用户名" + emp.getUsername() + "已存在");
        }
        //密码加密
        emp.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        emp.setStatus(1);
        //已经将id 存到ThreadLocal中  自动填充
        //emp.setCreateTime(LocalDateTime.now());
        //emp.setCreateUser((long) session.getAttribute("employee"));
        //emp.setUpdateTime(LocalDateTime.now());
        //emp.setUpdateUser((long) session.getAttribute("employee"));
        boolean flag = save(emp);
        return flag ? R.success("新增成功") : R.error("新增失败");
    }

    /**
     * 分页条件查询
     *
     * @param page     当前页码
     * @param pageSize 每页显示条数
     * @param name     员工名字
     * @return 分页的数据
     */
    @Override
    public R<Page<Employee>> conditionPage(Integer page, Integer pageSize, String name) {
        Page<Employee> employeePage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name != null && name.length() != 0, Employee::getName, name);

        Page<Employee> findPage = this.page(employeePage, wrapper);

        return R.success(findPage);
    }

    /**
     * 修改用户
     *
     * @param emp
     * @return
     */
    @Override
    public R<String> updateEmployee(Employee emp) {
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getUsername, emp.getUsername());
        wrapper.ne(emp.getId() != null, Employee::getId, emp.getId());
        int count = this.count(wrapper);
        if (count > 0 && StringUtils.isNotBlank(emp.getUsername())) {
            throw new CustomException("用户名已存在");
        }
        boolean flag = this.updateById(emp);
        return flag ? R.success("更新成功") : R.error("更新失败");
    }

    /**
     * 根据id回显数据
     *
     * @param id
     * @return
     */
    @Override
    public R<Employee> findById(long id) {
        Employee emp = this.getById(id);
        return R.success(emp);
    }
}
